//file:noinspection ALL
pipeline {
    agent any

    environment {
        projectName = 'api-gateway'
        // 源码配置
        git = 'http://git.yvanui.com/lizhiwei/api-gateway.git'
        gitCredentials = 'lizhiwei(git.yvanui.com)'
        // 构建配置
        baseDir = "${WORKSPACE}/api-gateway-server"
        dockerImgTag = "$projectName:v$BUILD_NUMBER"
        dockerfileTarget = 'test'
        // harbor配置
        harborProtocol = 'http://'
        harborIP = '192.168.1.211:30001'
        harborCredentials = credentials('admin(192.168.1.211:30001)')
        harborRepositorie = 'app'
        // 部署配置
        imagePullSecrets = 'harbor-secret'
        k8sNamespace = 'app'
        springProfiles = "k8s"
        containerPort = '8080'
        nodePort = '31000'
        logging = 'enable'
        // 自定义环境变量
        JAVA_HOME = '/data/jenkins-agent/env/java/jdk1.8.0_341'
        GRADLE_HOME = '/data/jenkins-agent/env/gradle/gradle-6.8.3'
        MAVEN_HOME = '/data/jenkins-agent/env/maven/apache-maven-3.8.6'
        NODE_HOME = '/data/jenkins-agent/env/nodejs/node-v16.18.0-linux-x64'
        PATH = "$JAVA_HOME/bin:$GRADLE_HOME/bin:$MAVEN_HOME/bin:$NODE_HOME/bin:$PATH"
    }

    stages {
        stage('#检查环境') {
            steps {
                sh """
                    java -version
                    gradle -v
                    helm version
                """
            }
        }

        stage('#拉取代码') {
            steps {
                checkout([
                        $class           : 'GitSCM',
                        branches         : [[name: '*/master']],
                        userRemoteConfigs: [[url: "${git}", credentialsId: "${gitCredentials}"]],
                        extensions       : [],
                        // extensions       : [[$class: 'RelativeTargetDirectory', relativeTargetDir: './']],
                ])
            }
        }

        stage('#编译项目') {
            steps {
                sh """
                    chmod +x gradlew
                    ./gradlew build -x test
                """
            }
        }

        stage('#构建镜像') {
            steps {
                sh """
                    cd $baseDir
                    pwd
                    sudo docker build --target $dockerfileTarget -t $dockerImgTag $baseDir
                """
            }
        }

        stage('#推送镜像') {
            steps {
                sh 'sudo docker login --username $harborCredentials_USR --password $harborCredentials_PSW $harborProtocol$harborIP'
                sh "sudo docker tag $dockerImgTag $harborIP/$harborRepositorie/$dockerImgTag"
                sh "sudo docker push $harborIP/$harborRepositorie/$dockerImgTag"
                sh "sudo docker system prune --filter 'until=72h' -f"
            }
        }

        stage('#部署服务') {
            steps {
                sh """
                    helm repo add lizw-repo https://gitee.com/LiZhiW/helm-chart/raw/master
                    helm repo update lizw-repo
                """
                script {
                    def deployCMD = new StringBuilder()
                    deployCMD.append("helm upgrade --install $projectName lizw-repo/java01")
                    deployCMD.append(" --namespace $k8sNamespace --create-namespace")
                    deployCMD.append(" --version 0.0.1")
                    deployCMD.append(" --set env.SPRING_PROFILES='$springProfiles'")
                    deployCMD.append(" --set env.JAVA_MEM_OPTS='-server -Xms128m -Xmx128m'")
                    deployCMD.append(" --set image='$harborIP/$harborRepositorie/$dockerImgTag'")
                    deployCMD.append(" --set imagePullSecrets='$imagePullSecrets'")
                    deployCMD.append(" --set containerPort='$containerPort'")
                    deployCMD.append(" --set nodePort='$nodePort'")
                    deployCMD.append(" --set labels.logging=$logging")
                    deployCMD.append(" --set nodeSelector.enable='true'")
                    deployCMD.append(" --set nodeSelector.values\\[0\\]='worker1'")
                    deployCMD.append(" --set livenessProbe.initialDelaySeconds='30'")
                    deployCMD.append(" --set readinessProbe.initialDelaySeconds='30'")
                    sh deployCMD.toString()
                }
            }
        }
    }
}
